package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.order.ITransferPlanService;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttrDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosGoodsDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.TransferPlanVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 组装报文传输计划节点
 * 
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl <br>
 */
@Service
public class BcmcPosTransferPlanHandler extends AbstractBcmcPosHandler {

    @Override
    public void doHandle(BcmcPosContext context) {

        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String mainOfferInstId = querySceneInstService.getExtParameterValue(context.getSceneInstId(),
            "mainOfferInstId");
        List<KitePosGoodsDTO> kitePosGoods = context.getPosDto().getGoods();
        kitePosGoods.forEach(good -> {
            // 核查转定、订单签订和预约传输计划变更时，good.getInstanceId()取到的是订单中心生成的实例ID，这里还需要判断跟极简的instanceId属性值相同
            if (KiteStringUtils.isEqual(mainOfferInstId, good.getInstanceId())
                || KiteStringUtils.isEqual(mainOfferInstId, good.getAttrValue("instanceId"))) {

                List<TransferPlanVO> TransferPlanVOs = SpringUtil.getBean(ITransferPlanService.class)
                    .getTransferPlanInfo(context.getSceneInstId());

                TransferPlanVOs.forEach(transferPlan -> {
                    KitePosAttrDTO attr = new KitePosAttrDTO();
                    attr.setCode("GRP_124_" + transferPlan.getTransferTaskInstId());
                    List<KitePosAttrDTO> transferPlanAttr = new ArrayList<>();
                    transferPlan.getPlanAttrs().stream().forEach(palnAttr -> {
                        KitePosAttrDTO attrDTO = new KitePosAttrDTO();
                        attrDTO.setCode(palnAttr.getFieldName());
                        attrDTO.setType("STRING");
                        attrDTO.setStartDate(KiteDateUtils.formatDate(
                            querySceneInstService.getSceneInst(context.getSceneInstId()).getCreateDate(),
                            KiteDateUtils.DATE_TIME_FORMAT_14));
                        attrDTO.setEndDate(LocalKeyConsts.ATTR_SEND_DATE);
                        attrDTO.setValue(getAttrValue(palnAttr));
                        transferPlanAttr.add(attrDTO);
                    });
                    attr.setValue(JSON.toJSONString(transferPlanAttr));
                    attr.setType("GROUP");
                    attr.setStartDate(KiteDateUtils.formatDate(
                        querySceneInstService.getSceneInst(context.getSceneInstId()).getCreateDate(),
                        KiteDateUtils.DATE_TIME_FORMAT_14));
                    attr.setEndDate(LocalKeyConsts.ATTR_SEND_DATE);
                    // 传输计划放到主商品下的产品
                    good.getProducts().get(0).getAttrs().add(attr);
                });

            }
        });
    }

    private String getAttrValue(InstAttrDTO kiteAttr) {
        if (!KiteStringUtils.isEmpty(kiteAttr.getDefaultValue())) {
            return kiteAttr.getDefaultValue();
        }
        return kiteAttr.getValue() == null ? "" : kiteAttr.getValue();
    }

}
